//
//  YLRelevantAgreementViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/24.
//  Copyright © 2017年 she1323. All rights reserved.
//  相关协议

#import "YLRelevantAgreementViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLRelevantAgreementItem.h"
@implementation YLRelevantAgreementViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    
    __weak typeof(self) weakSelf = self;
    
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSString *url = [NSString stringWithFormat:@"%@Product/PGetProductAgreements", YLURL];
            
            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
            params[@"ProductCode"] = input;
            params[@"AgreementID"] = @(-1);
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                                
                [WHProgressHUD dismiss];
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLRelevantAgreementItem *item = [YLRelevantAgreementItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    
                    weakSelf.models = item.item;
                    [subscriber sendNext:nil];
                    
                } else {
                    [subscriber sendNext:nil];
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
                [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            
            
            return nil;
        }];
    }];
}

- (NSArray *)models
{
    if (!_models) {
        _models = [NSArray array];
    }
    return _models;
}

-(void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}

@end
